package com.example.democlient;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.democlient.network.SocketService;
import com.example.democlient.network.serverpackets.SM_USER_AUTH_REQUEST;

public class MainActivity extends Activity implements OnClickListener {

	public Handler myHandler;
	private Button loginBtn;
	private Button registerBtn;
	private EditText accountEditText;
	private EditText passwordEditText;
	private MyApplication app;
	//定义Handler,    
    Handler mainHandler = new Handler() {  
        @Override  
        public void handleMessage(Message msg) { 
        	
            switch (msg.what) {  
            case 1:  
            	app.account = accountEditText.getText().toString();
            	Toast.makeText(MyApplication.mDemoApp.getApplicationContext(), "验证通过,用户名为:"+msg.obj,Toast.LENGTH_LONG).show();
            	Intent toIntent = new Intent();
    			toIntent.setClass(MainActivity.this,
    			TaskActivity.class);
    			startActivity(toIntent);
            break;  
            case 2:  
            	Toast.makeText(MyApplication.mDemoApp.getApplicationContext(), "账号不存在.",Toast.LENGTH_LONG).show();
            break;  
            case 0: 
            	Toast.makeText(MyApplication.mDemoApp.getApplicationContext(), "密码错误.",Toast.LENGTH_LONG).show();
            case 999: 
            	Toast.makeText(MyApplication.mDemoApp.getApplicationContext(), "连接服务器失败.",Toast.LENGTH_LONG).show();
            	Intent toIntent1 = new Intent();
    			toIntent1.setClass(MainActivity.this,MainActivity.class);
    			startActivity(toIntent1);
            break;  
            default:  
                break;  
            }  
            super.handleMessage(msg);  
        }  
    };  
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        SocketService.getInstance().startup();
        setContentView(R.layout.activity_service_login);
        loginBtn = (Button)findViewById(R.id.loginBtn);
        loginBtn.setOnClickListener(this);
        registerBtn = (Button)findViewById(R.id.registerBtn);
        registerBtn.setOnClickListener(this);
        accountEditText = (EditText)findViewById(R.id.accountEditText);
        passwordEditText = (EditText)findViewById(R.id.passwordEditText);
        app = (MyApplication)this.getApplication();
        app.addActivity(this);
    }
	
	@Override
	public void onStart(){
		super.onStart();
		app = (MyApplication)this.getApplication();
        app.setHandler(this.getClass(),mainHandler);
	}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    public boolean onOptionsItemSelected(MenuItem item){
    	super.onOptionsItemSelected(item);
    	if(item.getItemId()==R.id.menu_settings){
    		app.exit();
    	}
		return true;
    }

	@Override
	public void onClick(View v) {
		if(v.getId()==R.id.loginBtn ){
			String accountEditTextStr = this.accountEditText.getText().toString();
			String passwordEditText = this.passwordEditText.getText().toString();
			if(passwordEditText.equals("")||accountEditTextStr.equals("")){
				Toast.makeText(MyApplication.mDemoApp.getApplicationContext(), "信息不完整.",Toast.LENGTH_LONG).show();
				return;
			}
			if(!SocketService.getInstance().sendPacket(new SM_USER_AUTH_REQUEST(accountEditTextStr,passwordEditText), app)){
				Toast.makeText(MyApplication.mDemoApp.getApplicationContext(), "连接失败.",Toast.LENGTH_LONG).show();
				return;
			}
		}else if(v.getId()==R.id.registerBtn ){
			Intent toIntent = new Intent();
			toIntent.setClass(MainActivity.this,
			RegisterActivity.class);
			startActivity(toIntent);
		}
	}
    
}
